import * as THREE from 'three'
import { ColorGUIHelper } from './GUIHelper'
import { GUI } from 'three/addons/libs/lil-gui.module.min.js'
// 感觉 lookat 更合适，改变sun的位置

// todo 这个可能要改
// 使用 lookat 方法
export const addDirectionalLight = function ({
  color = new THREE.Color(0xffffff),
  intensity = 2,
  scene = window.threeScene,
  gui,
} = {}) {
  const sunLight = new THREE.DirectionalLight(color, intensity)
  const arrowDirection = new THREE.Vector3()
  const arrowPosition = new THREE.Vector3()
  // sunLight.position.set(3, 7, 0)
  sunLight.lookAt(scene.position)

  arrowDirection.subVectors(scene.position, sunLight.position).normalize()

  arrowPosition.copy(sunLight.position)
  const arrowHelper1 = new THREE.ArrowHelper(arrowDirection, arrowPosition, 3, 0xffff00, 0.25, 0.08)

  if (gui) {
    const container = document.querySelector('.gui-list')
    const g = new GUI({ title: 'directional light', container })
    g.addColor(new ColorGUIHelper(sunLight, 'color'), 'value').name('color')
    g.add(sunLight, 'intensity', 0, 5, 0.01)
    g.add(sunLight.position, 'x', -10, 10).onChange(() => {
      arrowDirection.subVectors(scene.position, sunLight.position).normalize()
      arrowHelper1.setDirection(arrowDirection)
    })
    g.add(sunLight.position, 'y', 0, 10).onChange(() => {
      arrowDirection.subVectors(scene.position, sunLight.position).normalize()
      arrowHelper1.setDirection(arrowDirection)
    })
    g.add(sunLight.position, 'z', -10, 10).onChange(() => {
      arrowDirection.subVectors(scene.position, sunLight.position).normalize()
      arrowHelper1.setDirection(arrowDirection)
    })
  }
  scene.add(sunLight)
  scene.add(arrowHelper1)
  return sunLight
}

/*
export class SunLight {
  constructor({ color = new THREE.Color(), intensity, scene, guiHelper }) {
    this.color = color
    this.intensity = intensity
    this.scene = scene
    this.guiHelper = guiHelper
    this.sunLight = getDirectionalLight({ color, intensity, scene, guiHelper })
  }

  update() {
    // 更新sunlight的方向
    const arrowDirection = new THREE.Vector3()
    const arrowPosition = new THREE.Vector3()
    arrowDirection.subVectors(this.scene.position, this.sunLight.position).normalize()
    arrowPosition.copy(this.sunLight.position)
  }
}
  */
